<?php
namespace app\common\validate;
use think\facade\Db;
use think\Validate;

class People extends Validate
{

    protected $rule=[
        'id'    =>'require',
        'name'    =>'require',
        'mobile'    =>'require|checkmobile',
        'card_num'    =>'require',
        'card_address'    =>'require',
        'unit_name'    =>'require',
        'unit_position'    =>'require',

    ];
    protected $message = [
        'id.require'    => '关键参数不能为空',

        'name.require'    => '姓名不能为空',
        'mobile.require'    => '手机号不能为空',
        'card_num.require'    => '身份证号不能为空',
        'card_address.require'    => '身份证地址不能为空',
        'unit_name.require'    => '单位名称不能为空',
        'unit_position.require'    => '工作职位不能为空',
    ];
    protected $scene = [
        'add' =>['name','mobile','card_num','card_address','unit_name','unit_position'],
        'edit' =>['id','name','mobile','card_num','card_address','unit_name','unit_position'],
    ];

    protected function checkmobile($value,$rule,$data){
        if(!is_numeric($value)){
            return '请输入数字';
        }
        if(strlen($value) != 11){
            return '请输入11位手机号';
        }
        if (!preg_match("/^1[3456789]\d{9}$/", $value)) {
            return '手机号码格式不正确';
        }
        return true;
    }
    protected function checkuniquemobile($value,$rule,$data){
        $one = DB::name('users')->where('mobile','=',$value)->find();
        if($one){
            return '手机号已经存在';
        }
        return true;
    }



}